﻿using log4net;
using SQLDataUploadHelper.daos;
using SQLDataUploadHelper.models.domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace SQLDataUploadHelper.services
{
    /// <summary>
    /// 数据库遍历服务
    /// </summary>
    internal class SQLErgodicService
    {
        private readonly ILog log = LogManager.GetLogger(App.LogRepository.Name, typeof(SQLErgodicService));
        private SqlHandleDao sqlHandleDao = new SqlHandleDao();

        public SQLErgodicService(SqlConnectConfig sqlConnectInfo)
        {
            sqlHandleDao.SqlConnectInfo = sqlConnectInfo;
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <param name="mysqlConnectInfo"></param>
        /// <returns></returns>
        public bool TestConnect()
        {
            string connectCommond = sqlHandleDao.SqlConnectInfo.getConnectCommond();
            log.Debug(connectCommond);
            SqlConnection connection = new SqlConnection(connectCommond);
            bool connectResult = false;
            try
            {
                connection.Open();
                connectResult = connection.State == System.Data.ConnectionState.Open;
            }
            catch (Exception ex)
            {
                log.Warn(ex.ToString());
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                }
            }
            return connectResult;
        }

        /// <summary>
        /// 获取所有表
        /// </summary>
        /// <returns></returns>
        public List<string> getTables()
        {
            return sqlHandleDao.GetTables();
        }

        /// <summary>
        /// 获取未上送的数据
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public DataTable? selectData(string tableName, int pageNum, int pageSize = 100)
        {
            return sqlHandleDao.selectData(tableName, pageNum, pageSize);
        }

        /// <summary>
        /// 更新上送状态
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool updateUploadStatus(string tableName, List<string> ids)
        {
            return sqlHandleDao.updateById(tableName, ids);
        }

        /// <summary>
        /// 获取列名
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public List<string> getCloumnsName(DataTable dataTable)
        {
            List<string> columns = new List<string>();
            if (dataTable.Columns.Count > 0)
            {
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    columns.Add(dataTable.Columns[i].ColumnName);
                }
            }
            return columns;
        }

    }
}
